package com.fanqwz.hexieshe.ui;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.fanqwz.hexieshe.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by fanqw on 2017/7/10.
 */

public class SwipeRefreshLoadLayout extends SwipeRefreshLayout {
    private OnLoadLisener onLoadLisener;
    private boolean isLoaded = true;
    private float lastDown = 0;
    private float mTotalUnconsumed;

    @Bind(R.id.drag_to_show_more)
    View showMoreView;

    public SwipeRefreshLoadLayout(Context context) {
        super(context);
        init(context);
    }

    public SwipeRefreshLoadLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        setClipChildren(false);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        showMoreView.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(getResources().getDimensionPixelSize(R.dimen.show_more_view_height), MeasureSpec.EXACTLY));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        showMoreView.layout(left, bottom, right, bottom + getResources().getDimensionPixelSize(R.dimen.show_more_view_height));
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.bind(this, this);
        ViewGroup viewGroup = (ViewGroup) showMoreView.getParent();

        while (viewGroup != null && viewGroup != this) {
            viewGroup.setClipChildren(false);
            viewGroup = (ViewGroup) viewGroup.getParent();
        }
    }

    public void setOnLoadListener(OnLoadLisener onLoadListener) {
        this.onLoadLisener = onLoadListener;
    }

    public void setLoaded() {
        if (!isLoaded) {
            doHideLoadViewAnimation();
            scrollTo(0, 0);
        }

        isLoaded = true;
    }

    private void onSwipeToLoad() {
        isLoaded = false;
        doShowLoadViewAnimation();
        onLoadLisener.onLoad();
    }

    private void doHideLoadViewAnimation() {
//        showMoreView.setVisibility(GONE);
        scrollTo(0, 0);
    }

    public void doShowLoadViewAnimation() {
//        showMoreView.setVisibility(VISIBLE);
    }


    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        super.onNestedScrollAccepted(child, target, axes);
        mTotalUnconsumed = 0;
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        Log.e("111", "onNestedScroll: " + dxConsumed + "," + dyConsumed + "," + dxUnconsumed + "," + dyUnconsumed);
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

        if (dyUnconsumed > 0 && isLoaded && mTotalUnconsumed <= showMoreView.getHeight()) {
            mTotalUnconsumed += Math.abs(dyUnconsumed);
            scrollBy(0, dyUnconsumed);
        } else if (dyConsumed < 0 && getScrollY() > 0) { //上拉但是没有到底，往回滑动的情况
            mTotalUnconsumed += dyConsumed;
            scrollBy(0, getScrollY() + dyConsumed < 0 ? 0 : dyConsumed);
        }
    }

    @Override
    public void onStopNestedScroll(View target) {
        super.onStopNestedScroll(target);

        if (mTotalUnconsumed > showMoreView.getHeight() / 3 * 2) {
            onSwipeToLoad();
        } else if (getScrollY() > 0) {
            doHideLoadViewAnimation();
        }
    }

    public interface OnLoadLisener {
        void onLoad();
    }
}
